<?php

namespace App\Models\Order;

use App\Models\Balance\Balance as ModelsBalance;
use App\Models\Custom\Custom;
use App\Models\Fee\FeeType;
use App\Models\Manger\Manger;
use App\Models\Settle\SettleCompany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SaleorderOtherfeePay extends Model
{
    protected $table = 'sale_order_otherfee_pay';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    public static function getList($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::with(['pminfo','feeinfo','paybank','approve'])->orderBy('pay_at', 'desc');

        if($memberinfo['utype'] == 7){
            $query->where('pay_mid', '=', $manger_id);
        }
        $query->where('cpid', $cpid);

        $other_pid = $request->id; //费用付款单-父级，其他费用数据
        if($other_pid > 0){
            $query->where('other_pid', '=', $other_pid);
        }

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }
        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);
        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }

        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }

        $settlement_company = $request->settlement_company;
        if ($settlement_company) {
            $query->where('pay_bank', $settlement_company);
        }

        $status = $request->status;
        if ($status > 0) {
            $query->where('status', $status);
        }


        $pmname = $request->pmname;
        if ($pmname) {
            $query->whereHas('pminfo', function ($query) use ($pmname) {
                $query->where('realname', $pmname);
            });
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public static function getListbalance($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;

        $query = self::with(['companyinfo','paybank','approve'])->orderBy('id', 'desc');

        if($memberinfo['utype'] == 7){
            $query->where('pay_mid', '=', $manger_id);
        }
        $query->where('cpid', $cpid);

        $settlement_company = $request->settlement_company;
        if ($settlement_company) {
            $query->where('settlement_company', 'like','%'.$settlement_company.'%');
        }

        $company_id = $request->company_id;
        if (!empty($company_id)) {
            $query->whereHas('companyinfo', function ($query) use ($company_id) {
                $query->where('id', '=', $company_id);
            });
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            $relist = paginateDataToArray($list);
            $relist['balance'] = 0;
            if (!empty($company_id)) {
                $balance = ModelsBalance::where(['custom_id' => $company_id])->value('balance');
                $relist['balance'] = $balance;
            }
            return $relist;
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
    /***
     *   出纳端
     *
     *
    */
    public static function getListBycn($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::whereHas('approve', function ($q){
            $q->where('status', '3');
        })->with("approve")->with(["pminfo","feeinfo","paybank"])->where('cpid',$cpid)->orderBy('pay_at', 'desc');

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }
        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);
        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }


        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }

        $pmname = $request->pmname;
        if ($pmname) {
            $query->whereHas('pminfo', function ($query) use ($pmname) {
                $query->where('realname', 'like', '%'.$pmname.'%');
            });
        }



        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
    /***
     *   统审
     *
     *
    */
    public static function getListByTs($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        $query = self::whereHas("approve",function($q) use($manger_id,$cpid){
            $q->where(['ts_mid'=>$manger_id,'cpid'=>$cpid]);
        })
            ->with("approve")->with(["pminfo","feeinfo","paybank"])
            ->where('cpid',$cpid)
            ->orderBy('pay_at', 'desc');

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }


        $ft_name = $request->ft_name;
        if ($ft_name!='') {
            $query->where('fee_name', 'like','%'.$ft_name.'%');
        }


        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);

        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }

        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }

        // $status = $request->status;
        // if ($status > 0) {
        //     $query->where('status', $status);
        // }





        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
    /***
     *  操盘
     *
    */
    public static function getListByCp($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        //$query = self::with(['feeinfo','paybank'])->orderBy('pay_at', 'desc');
        $query = self::whereHas("approve",function($q) use ($manger_id,$cpid){
            $q->whereIn("status",['2','3','4','5'])->where(['cp_mid'=>$manger_id,'cpid'=>$cpid]);
        })->with("approve")->with(["pminfo","feeinfo","paybank"])
            ->where(['cpid'=>$cpid])
            ->orderBy('pay_at', 'desc');

        $ft_id = $request->ft_id;
        if ($ft_id > 0) {
            $query->where('fee_type_id', $ft_id);
        }

        $ft_name = $request->ft_name;
        if ($ft_name!='') {
            $query->where('fee_name', 'like','%'.$ft_name.'%');
        }

        $jj_mark = $request->jj_mark;
        if ($jj_mark) {
            $query->where('jj_mark', $jj_mark);
        }

        $fee_min=intval(floatval($request->fee_min)*100);
        $fee_max=intval(floatval($request->fee_max)*100);
        if($fee_min>0 && $fee_max>0){
            $query->whereBetween('fee_num',[$fee_min,$fee_max]);
        }

        $custom_name = $request->custom_name;
        if ($custom_name) {
            $query->where('custom_name', 'like','%'.$custom_name.'%');
        }

        // $status = $request->status;
        // if ($status > 0) {
        //     $query->where('status', $status);
        // }





        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
    public function tsinfo(){
        return $this->belongsTo(Manger::class,'ts_mid','id');
    }

    public function cpinfo(){
        return $this->belongsTo(Manger::class,'cp_mid','id');
    }

    public function pminfo(){
        return $this->belongsTo(Manger::class,'pay_mid','id');
    }

    public function companyinfo(){
        return $this->belongsTo(Custom::class,'company_id','id')->select(['id', 'company_name', 'company_code']);
    }

    public function feeinfo(){
        return $this->belongsTo(FeeType::class,'fee_id','id');
    }

    public function paybank(){
        return $this->belongsTo(SettleCompany::class,'pay_bank','id');
    }
    public function otherfee_info(){
        return $this->belongsTo(Saleorderfee::class,'id','otherfee_id');
    }

    public function approve(){
        return $this->hasOne(SaleorderOtherfeePayApprove::class,'otherfee_pay_id','id');
    }
}
